package com.hw.one.peach.da.service;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.component.data.transform.excel.Import;
import com.hw.one.core.service.PageService;
import com.hw.one.device.dao.ISiteV2Dao;
import com.hw.one.peach.da.mongo.IAirMongo;
import com.hw.one.unifiedcode.po.UnifiedCodePo;
import com.hw.one.unifiedcode.service.IUnifiedcodeService;

/**
  * @author  sjd 
  * @date 创建时间：2017年7月28日 下午3:12:20 
  * @version 1.0.0
  */
@Service(value="peachAirService")
public class PeachAirService extends PageService implements IPeachAirService {

	@Resource
	private IAirMongo airMongo;
	
	@Resource
	private IUnifiedcodeService unifiedcodeService;
	
	@Resource
	private ISiteV2Dao siteV2Dao;
	
	@Override
	public void upload(Map<String, Object> params, Map<String, MultipartFile> files) throws Exception {
		for (String file : files.keySet()) {
			Import imp = new Import();
			
			// {"Sheet1":{"air":[{"地市":"","区县":"","站点":"","风速":"","风向":"","气温":""}, {"地市":"","区县":"","站点":"","风速":"","风向":"","气温":""}}]}
			InputStream xml = this.getClass().getClassLoader().getResourceAsStream("ext/tpl/excel/da/tpl-air-hour-import.xml");
			Map<String, Object> exceldata = imp.excelToData(xml, files.get(file).getInputStream(), files.get(file).getOriginalFilename());
			
			List<Map<String, Object>> rows = ((List<Map<String, Object>>)((Map<String, Object>)exceldata.get(exceldata.keySet().toArray()[0])).get("air"));
			
			// 转换编码dictcode转为mycode
			JSONArray dicts = new JSONArray();
			JSONObject mapd = new JSONObject();
			for (String key : rows.get(0).keySet()) {
				mapd = new JSONObject();
				mapd.put("inCode", key);
				mapd.put("catalogCode", "ITP_AIR");
				dicts.add(mapd);
			}
			
			Map<String, Object> dictparam = new HashMap<String, Object>();
			dictparam.put("data", dicts);
			List<UnifiedCodePo> results = unifiedcodeService.getCodeInfoList(dictparam);
			
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			Map<String, Object> dta = new HashMap<String, Object>();

			// 校验日期格式
			String monitorTime = "";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss");
			
			// 查询系统中维护的国控站点
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("isMove", "0");
			param.put("siteTypeCode", "410");
			List<Map<String, Object>> contrysites = siteV2Dao.get(param);
			
			for (Map<String, Object> map : rows) {
				dta = new HashMap<String, Object>();
				for (String key : map.keySet()) {
					if (key.equals("monitorTime")) {
						monitorTime = String.valueOf(map.get(key));
						try {
							sdf.parse(monitorTime);
						} catch (Exception e) {
							throw new Exception("日期格式不正确，应改为：yyyy-MM-dd HH:ss");
						}
					}
					// 将导入数据中的站点名称转换为系统中维护的国控站的站点编码
					if (key.equals("siteCode")) {
						boolean flag = false;
						for (Map<String, Object> map2 : contrysites) {
							if (String.valueOf(map2.get("siteName")).equals(String.valueOf(map.get("siteCode")))) {
								map.put("siteCode", map2.get("siteCode"));
								flag = true;
								break;
							}
						}
						if (!flag) {
							throw new Exception("系统中未维护此国控站：" + map.get("siteCode"));
						}
					}
					for (UnifiedCodePo po : results) {
						if (key.equals(po.getInCode())) {
							dta.put(po.getMyCode(), map.get(key));
							break;
						}
					}
				}
				data.add(dta);
			}
			
			List<Map<String, Object>> sss = new ArrayList<Map<String, Object>>();
			Map<String, Object> result = new HashMap<String, Object>();
			List<Map<String, Object>> pollutants = new ArrayList<Map<String, Object>>();
			Map<String, Object> temp = new HashMap<String, Object>();
			for (Map<String, Object> map : data) {
				result = new HashMap<String, Object>();
				pollutants = new ArrayList<Map<String, Object>>();
				for (String key : map.keySet()) {
					temp = new HashMap<String, Object>();
					if (key.startsWith("a02")) {
						temp.put("PollutantCode", key);
						temp.put("MonValue", map.get(key));
						temp.put("Mark", "");
						
						pollutants.add(temp);
					} else {
						result.put(key, map.get(key));
					}
				}
				result.put("pollutant", pollutants);
				sss.add(result);
			}
			for (Map<String, Object> map : sss) {
				airMongo.upload(map, "IMP_ITP_AIR_HOUR-" + String.valueOf(map.get("monitorTime")).substring(0, 4));
			}
		}
	}
}
